package com.demo.bookstore.test.data.util;

import com.demo.bookstore.mapper.BookEntityManualMapper;
import com.demo.bookstore.model.BookEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@SpringBootTest
@RunWith(SpringRunner.class)
public class CreateTestData {
    @Autowired
    private BookEntityManualMapper bookEntityManualMapper;
    private Random random = new Random(System.currentTimeMillis());
    @Test
    public void createData(){
        int batchSize=1000;
        IntStream.range(0,100).forEach((i)->{
            List<BookEntity> bookEntities = IntStream.range(0,batchSize).mapToObj(j -> generateBookEntity(i*batchSize + j)).collect(Collectors.toList());
            bookEntityManualMapper.batchInsert(bookEntities);
        });
    }

    private BookEntity generateBookEntity(int index){
        List<String> language= Arrays.asList("JAVA", "C语言", "C#", "C++", "Mybatis", "Spring Boot", "Spring Cloud", "Go语言", "PostgreSql", "Redis");
        List<String> function = Arrays.asList("基本原理", "编程实战", "并发编程", "源码解析", "从入门到精通");
        String generateName = language.get(random.nextInt(language.size()))+ function.get(random.nextInt(function.size()))+index;
        BookEntity bookEntity = new BookEntity();
        bookEntity.setName(generateName);
        bookEntity.setDescription(generateName);
        bookEntity.setClassification("编程");
        bookEntity.setPublishDate(new Date());
        return bookEntity;
    }


}
